package insulin

import (
	"github.com/bgmp/config"
	"github.com/bgmp/handler"
	"github.com/bgmp/model"
	"github.com/gin-gonic/gin"
)

func (b Base) AddInsulinTherapy(c *gin.Context, req *AddInsulinTherapyRequest) *AddInsulinTherapyResponse {
	if req.PatientId == 0 {
		handler.Errorf(c, "请设置病人ID")
		return nil
	}

	user := model.User{}
	user.ID = req.PatientId
	err := user.Get(config.DB)
	if err != nil {
		handler.Errorf(c, "no such patient")
		return nil
	}

	therapy := req.ToTherapy()
	err = therapy.Save(config.DB)
	if err != nil {
		handler.Errorf(c, err.Error())
		return nil
	}
	return &AddInsulinTherapyResponse{}
}

type AddInsulinTherapyRequest struct {
	PatientId      uint       `json:"patient_id"`
	SaveTime       model.Time `json:"save_time"`
	Tdi            float32    `json:"tdi"`
	BasalTdi       float32    `json:"basal_tdi"`
	BolusTdi       float32    `json:"bolus_tdi"`
	BasalInject    float32    `json:"basal_inject"`
	Pump_0_3       float32    `json:"pump_0_3"`
	Pump_3_8       float32    `json:"pump_3_8"`
	Pump_8_12      float32    `json:"pump_8_12"`
	Pump_12_16     float32    `json:"pump_12_16"`
	Pump_16_20     float32    `json:"pump_16_20"`
	Pump_20_24     float32    `json:"pump_20_24"`
	BreakfastBolus float32    `json:"breakfast_bolus"`
	LunchBolus     float32    `json:"lunch_bolus"`
	DinnerBolus    float32    `json:"dinner_bolus"`
	BedtimeBasal   float32    `json:"bedtime_basal"`
}

func (req *AddInsulinTherapyRequest) ToTherapy() *model.InsulinTherapy {
	return &model.InsulinTherapy{
		UserId:         req.PatientId,
		SaveTime:       req.SaveTime.Time,
		Tdi:            req.Tdi,
		BasalTdi:       req.BasalTdi,
		BolusTdi:       req.BolusTdi,
		BasalInject:    req.BasalInject,
		Pump_0_3:       req.Pump_0_3,
		Pump_3_8:       req.Pump_3_8,
		Pump_8_12:      req.Pump_8_12,
		Pump_12_16:     req.Pump_12_16,
		Pump_16_20:     req.Pump_16_20,
		Pump_20_24:     req.Pump_20_24,
		BreakfastBolus: req.BreakfastBolus,
		LunchBolus:     req.LunchBolus,
		DinnerBolus:    req.DinnerBolus,
		BedtimeBasal:   req.BedtimeBasal,
	}
}

type AddInsulinTherapyResponse struct {
}
